package com.quanyan.club.operate.service;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.request.ReqUserAccumulateModify;
import com.quanyan.assets.response.RespUserAccumulateModify;
import com.quanyan.assets.service.UserAssetsService;
import com.quanyan.club.constants.ClubConstants;
import com.quanyan.club.entity.db.TbOperateLotteryPrizeItems;
import com.quanyan.club.entity.db.TbOperateLotteryPrizeRecord;
import com.quanyan.club.mapper.TbOperateLotteryPrizeRecordMapper;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.user.response.RespUserInfoBase;

/**
 * Created by zhuo.shi on 2016/12/8.
 *
 * 抽奖活动，中奖后处理类
 */
public class GotPrizeProcessThread implements Runnable{

    private boolean hitPrize;           //是否中奖
    private long costEggs;
    private RespUserInfoBase userInfo;
    private TbOperateLotteryPrizeRecordMapper lotteryPrizeRecordMapper;
    private TbOperateLotteryPrizeItems prize;
    private RedisTemplate redisTemplate;
    private UserAssetsService userAssetsService;

    public GotPrizeProcessThread(RespUserInfoBase userInfo, TbOperateLotteryPrizeRecordMapper lotteryPrizeRecordMapper, TbOperateLotteryPrizeItems prize,
                                 RedisTemplate redisTemplate, UserAssetsService userAssetsService, long costEggs, boolean hitPrize){
        this.userAssetsService = userAssetsService;
        this.redisTemplate = redisTemplate;
        this.lotteryPrizeRecordMapper = lotteryPrizeRecordMapper;
        this.userInfo = userInfo;
        this.prize = prize;
        this.hitPrize = hitPrize;
        this.costEggs = costEggs;
    }

    @Override
    public void run() {
        //记录抽奖信息
        TbOperateLotteryPrizeRecord record = new TbOperateLotteryPrizeRecord();
        record.setCostEggs((int)costEggs);
        record.setMobile(userInfo.getMobile());
        record.setUid(userInfo.getUid());
        record.setPrizeItemId(prize.getId());
        record.setPrizeName(prize.getPrizeDesc());
        if (hitPrize) {     //如果中奖了
            record.setHitPrize(true);
        }else {
            record.setHitPrize(false);
        }
        lotteryPrizeRecordMapper.insertSelective(record);
        //记录中奖信息
        if (hitPrize) {
            if (prize.getLevel() == 3) {        //安慰奖：发放鸟蛋20个
                giveOutEggs(userInfo);
            }

            redisTemplate.lpush("club:lottery:hitprize", JSON.toJSONString(record));   //中奖用户列表放入缓存
        }
    }

    private void giveOutEggs(RespUserInfoBase userInfo){
        ReqUserAccumulateModify modify = new ReqUserAccumulateModify();
        modify.setUid(userInfo.getUid());
        modify.setAccumulate(20L);
        modify.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        modify.setSystemCode(Constants.CLUB_SYSTEM_CODE);
        modify.setBusinessCode(ClubConstants.CLUB_EGG_WEEK_TASK_CODE);
        String userName = userInfo.getNickName();
        modify.setReceiver(userName);
        modify.setPayReceive("系统发放");
        modify.setInfo("俱乐部抽奖活动");

        APIRequest<ReqUserAccumulateModify> apiRequest = new APIRequest<>();
        apiRequest.setData(modify);

        APIResponse<RespUserAccumulateModify> response =  userAssetsService.modifyAccumulate(apiRequest);
        if (!response.isRet()){
            throw new BizException("发放"+20+"个鸟蛋给俱乐部主席"+userName+"失败，"+response.getErrmsg());
        }
    }
}
